<?php

namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\VipPackage\StoreVipPackageRequest;
use App\Http\Requests\Admin\VipPackage\UpdateVipPackageRequest;
use App\Models\Company\VipPackage;
use Illuminate\Http\Request;
use Style;

class VipPackageController extends CommonController{
    /**
     * 显示会员
     * @return mixed
     */
    public function index(){
        $vipPackage = VipPackage::orderBy('updated_at','desc')->paginate(20);
        return Style::view('admin.company_manage.vip_package.index',compact('vipPackage'));
    }

    /**
     * 添加会员
     * @return mixed
     */
    public function create(){
        return Style::view('admin.company_manage.vip_package.create');
    }

    /**
     * 添加会员提交
     * @param StoreVipPackageRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(StoreVipPackageRequest $request){
        $input = $request->only(['vipname','vip_days','vip_resume_number','vip_integral','vip_post_number','resume_number','integral','money','stats','remark']);
        $result = VipPackage::create($input);
        if($result){
            flash('会员套餐管理添加成功', 'success');
            return redirect('admin/vip_package');
        }else{
            return back()->with('errors','会员套餐管理添加失败，请稍后重试！');
        }
    }

    /**
     * 显示会员
     * @param $id
     */
    public function show($id){ }

    /**
     * 编辑会员
     * @param $id
     * @return mixed
     */
    public function edit($id){
        $vipPackage = VipPackage::find($id);
        return Style::view('admin.company_manage.vip_package.edit',compact('vipPackage'));
    }

    /**
     * 更新会员
     * @param UpdateVipPackageRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(UpdateVipPackageRequest $request, $id){
        $input = $request->only(['vipname','vip_days','vip_resume_number','vip_integral','vip_post_number','resume_number','integral','money','stats','remark']);
        $result = VipPackage::where('id',$id)->update($input);
        if($result){
            flash('更新会员套餐成功', 'success');
            return redirect('admin/vip_package');
        }else{
            return back()->with('errors','更新前和更新后数据相同，更新失败！！');
        }
    }

    /**
     * 删除会员
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id){
        $result = VipPackage::where('id',$id)->delete();
        if($result){
            return response()->json(['status' => 0,'msg' => '删除会员套餐成功！']);
        }else{
            return response()->json(['status' => 1,'msg' => '删除会员套餐失败，请稍后重试！']);
        }
    }
}
